<?php

namespace App\Models\Office;

use App\Models\Area;
use App\Models\Hotel\Hotel;
use Carbon\Carbon;
use App\Models\BaseModel;
use App\Helpers\MoneyHelper;
use App\Format\BaseFormat;

/**
 * App\Models\Office\Office
 *
 * @property int $id
 * @property string $name 名称
 * @property string $thumbnail 封面
 * @property string $images 相册
 * @property int $province_id 省ID
 * @property string $province_text 省文本
 * @property int $city_id 市ID
 * @property string $city_text 市文本
 * @property int $district_id 区ID
 * @property string $district_text 区文本
 * @property string $address 详细地址
 * @property string $lng 经度
 * @property string $lat 维度
 * @property string $desc 简述
 * @property string $content 详情
 * @property int $is_delete 是否删除：1是 0否
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @property string $keyword 关键词
 * @property-read mixed $faddress
 * @method static \Illuminate\Database\Eloquent\Builder|Office apiBaseQuery($whereParams = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Office bgBaseQuery($whereParams = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Office newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Office newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel paramsQuery($fields, $params = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Office query()
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereCityText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereDistrictText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereIds($ids = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereImages($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereIsDelete($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereKeyword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereProvinceText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereThumbnail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Office whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Office extends BaseModel
{
    /**
     * Define table name
     *
     * @var string
     */
    protected $table = 'offices';

    /**
     * Define a primary key
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * A property that cannot be batched.
     *
     * @var array
     */
    protected $guarded = ['id'];
    protected $hidden  = ['updated_at'];
    protected $casts   = [
        'images' => 'array'
    ];

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        self::saved(function ($model) {
            /** @var self $model */
            if ($model->isDirty('province_id') || $model->isDirty('city_id') || $model->isDirty('district_id') || $model->isDirty('address')) {
                self::storeLocation($model);
            }

            if (
                $model->isDirty('name') ||
                $model->isDirty('province_id') ||
                $model->isDirty('city_id') ||
                $model->isDirty('district_id') ||
                $model->isDirty('address')
            ) {
                self::storeKeyword($model, [
                    $model->name, $model->faddress
                ]);
            }
        });
    }
    /*********************************************属性格式化***************************************************************/

    /*********************************************设置属性***************************************************************/

    /*********************************************模型操作***************************************************************/
    public function scopeBgBaseQuery($query, $whereParams = [])
    {
    }

    public function scopeApiBaseQuery($query, $whereParams = [])
    {
    }

    public static function getForms($fields = [], $type = 'search', $width = '300px')
    {
        $forms = [
            'name'      => ['title' => '名称', 'type' => 'text'],
            'thumbnail' => ['title' => '封面', 'type' => 'image'],
            'images'    => ['title' => '相册', 'type' => 'photo', 'num' => 9],
            'area_ids'  => ['title' => '区域', 'type' => 'treeSelect', 'options' => Area::getTree()],
            'address'   => ['title' => '详细地址', 'type' => 'text'],
            'desc'      => ['title' => '简介', 'type' => 'textarea'],
            'content'   => ['title' => '描述', 'type' => 'longText']
        ];
        return self::handleForms($forms, $fields, $type, $width);
    }
    /*********************************************模型关联***************************************************************/

}
